package acwing._2LanQiao._7Greedy;
/**
 * @Project : AlgorithmLearning
 * @Package : ACWing._2LanQiao._7Greedy
 * @File : Q0122.java
 * @Author : WangRuoyu
 * @Date : 2023/3/20 10:15
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Q0122 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();

        int n = Integer.parseInt(line);
        long[] a = new long[n];

        long s = 0;
        for (int i = 0; i < n; ++i) {
            line = br.readLine();
            a[i] = Long.parseLong(line);
            s += a[i];
        }

        s /= n;
        long[] c = new long[n];
        for (int i = 1; i < n; ++i) {
            c[i] = c[i - 1] - s + a[i - 1];
        }

        Arrays.sort(c);

        long mid = c[(n - 1) / 2];

        long res = 0;
        for (int i = 0; i < n; ++i) {
            res += Math.abs(c[i] - mid);
        }

        System.out.println(res);
    }
}
